//
// Created by MIKE on 2018/12/18.
//

#ifndef UPGRADE_MSGLIST_H
#define UPGRADE_MSGLIST_H

#include <stdio.h>
#include <iostream>
#include <stdbool.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/sem.h>

#define MSG_NUM_ERROR       0xFFFFFFFF

namespace bwcgi {

    typedef struct  _bw_msg_
    {
        long type;
        char content[100];
    }BWMsg;

    union semun {
        short val;              /*SETVAL用的值*/
        struct semid_ds* buf;   /*IPC_STAT、IPC_SET用的semid_ds结构*/
        unsigned short* array;  /*SETALL、GETALL用的数组值*/
        struct seminfo *_buf;   /*为控制IPC_INFO提供的缓存*/
    };

    class MsgList
    {
    public:
        MsgList(const char *path, int proId);

        virtual ~MsgList();

        bool sendMsg(const void *msg, size_t msgLen);

        bool getMsg(void *msg, size_t msgLen);

        void deleteMsgList();

        bool msgIsInit();

        int getSemVal();

        bool setSemVal(short val);

        U32 getMsgNum();
    protected:
        long getMsgType(const void *msg);

        bool initSemProducer();

        bool initSemCustomer();

        bool sem_v();
        bool sem_p();
    private:
        bool msgInit;
        bool semInit;
        key_t key;
        int msgId;
        int semId;
    };

}   /* namespace    */
#endif //UPGRADE_MSGLIST_H
